﻿using System;
using System.Collections.Generic;

namespace Cms.Domain
{
    [Serializable]
    public class Locale : IComparable<Locale>
    {
        private string _aliasDomain;
        private DateTime? _contentLatestUpdated;
        private string _cultureCode;
        private int _id;
        private Site _site;
        private IDictionary<int, LocaleTranslation> _translations;
        private string _urlCode;

        public Locale()
        {
            _translations = new Dictionary<int, LocaleTranslation>();
        }

        public virtual int ID
        {
            get { return _id; }
            set { _id = value; }
        }
        
        public virtual Site Site
        {
            get { return _site; }
            set { _site = value; }
        }
        
        public virtual string AliasDomain
        {
            get { return _aliasDomain; }
            set { _aliasDomain = value; }
        }

        public virtual string CultureCode
        {
            get { return _cultureCode; }
            set { _cultureCode = value; }
        }
        
        public virtual string UrlCode
        {
            get { return _urlCode; }
            set { _urlCode = value; }
        }
        
        public virtual DateTime? ContentLatestUpdated
        {
            get { return _contentLatestUpdated; }
            set { _contentLatestUpdated = value; }
        }
        
        public virtual IDictionary<int, LocaleTranslation> Translations
        {
            get { return _translations; }
            set { _translations = value; }
        }
        
        public virtual LocaleTranslation GetTranslation(string cultureCode)
        {
            // First, try to get a translation that mathes the selected admin language
            foreach (Locale l in _site.Locales)
                if (l.CultureCode == cultureCode)
                    return _translations[l.ID];

            // No match, return the default translation
            return _translations[_site.DefaultLocale.ID];
        }

        public virtual int CompareTo(Locale other)
        {
            return ID > 0 && ID == other.ID ? 0 : 1;
        }
    }
}